/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JanelaRelatorioVendas.java
 *
 * Created on 17/05/2011, 12:52:03
 */
package petshop.gui;

import java.util.GregorianCalendar;
import javax.swing.JOptionPane;
import petshop.classes.BancoDeDados;
import petshop.classes.Venda;

/**
 *
 * @author Priscila, Maria Camila, Lara
 */
public class JanelaRelatorioVendas extends javax.swing.JDialog {
    Venda[] vendas;

    /** Creates new form JanelaRelatorioVendas */
    public JanelaRelatorioVendas(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    JanelaRelatorioVendas() {
        initComponents();
    }
        
    private Venda[] getVendas(){
        Venda p = new Venda();
        
        GregorianCalendar data = new GregorianCalendar();        
        data.set(Integer.valueOf(this.anoText.getText()),Integer.valueOf(this.mesText.getText()),1);
        p.setData(data);
        
        return BancoDeDados.consultar(p);
        
    
}
    protected void pesquisa(){
        vendas = getVendas();
        double total=0;
        if(vendas.length == 0){
            JOptionPane.showMessageDialog(this, "A busca não retornou nenhum resultado!");
            return;
        }
        
        Object[][] dados = new Object[vendas.length][3];
        
        for(int i = 0; i < vendas.length; i++){
            dados[i][0] = vendas[i].getCodigo();
            dados[i][1] = vendas[i].getCliente().getNome();
            dados[i][2] = vendas[i].total();
            total=total+vendas[i].total();
        }
        
        tabelaRelatorio.setModel(new javax.swing.table.DefaultTableModel(
    dados,
    new String [] {"Codigo", "Cliente", "Valor"}));
        this.totalText.setText(String.valueOf(total));
        
    

        
    }
 

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        mesText = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        anoText = new javax.swing.JTextField();
        BotaoGerar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaRelatorio = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        totalText = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Mês");
        jLabel1.setMinimumSize(new java.awt.Dimension(30, 14));

        mesText.setMinimumSize(new java.awt.Dimension(35, 20));
        mesText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mesTextActionPerformed(evt);
            }
        });

        jLabel2.setText("Ano");

        anoText.setMinimumSize(new java.awt.Dimension(45, 20));

        BotaoGerar.setText("Gerar Relatório");
        BotaoGerar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                BotaoGerarMouseClicked(evt);
            }
        });

        tabelaRelatorio.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Codigo", "Cliente", "Valor"
            }
        ));
        jScrollPane1.setViewportView(tabelaRelatorio);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel3.setText("Total:");

        totalText.setFont(new java.awt.Font("Tahoma", 0, 18));
        totalText.setForeground(new java.awt.Color(255, 51, 51));
        totalText.setText("00.00");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 402, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(mesText, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(anoText, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(127, 127, 127)
                        .addComponent(BotaoGerar))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(totalText)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(mesText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2)
                        .addComponent(anoText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(BotaoGerar)))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(totalText))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BotaoGerarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_BotaoGerarMouseClicked
// TODO add your handling code here:
    if (mesText.equals("")&&anoText.equals("")){
        JOptionPane.showMessageDialog(null, "Campos mês e ano vazios!");
    } else if (anoText.equals("")){
        JOptionPane.showMessageDialog(null, "Campo ano vazio!");
        
    } else if (mesText.equals("")){
        JOptionPane.showMessageDialog(null, "Campo mês vazio!");
        
    }else
        pesquisa();
    }//GEN-LAST:event_BotaoGerarMouseClicked

    private void mesTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mesTextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_mesTextActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                JanelaRelatorioVendas dialog = new JanelaRelatorioVendas(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BotaoGerar;
    private javax.swing.JTextField anoText;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField mesText;
    private javax.swing.JTable tabelaRelatorio;
    private javax.swing.JLabel totalText;
    // End of variables declaration//GEN-END:variables
}
